import wx
import MySQLdb
import datetime
from threading import Thread

connection = MySQLdb.connect('localhost', 'user', 'password')
cursor = connection.cursor()
default_db = 'q_self'

def valid_date(input):
  try:
    valid = datetime.datetime.strptime(input, "%Y-%m-%d")
  except ValueError:
    valid = None
  return valid

def valid_time(input):
  try:
    valid = datetime.datetime.strptime(input, "%H:%M")
  except ValueError:
    valid = None
  return valid

class MainForm(wx.Frame):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(230, 130))

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2,2)

    database = wx.StaticText(panel, label='Database')
    self.database_pnl = wx.TextCtrl(panel)
    self.database_pnl.SetValue(default_db)

    tables = ['Food', 'Discretionary', 'Sleep', 'Social', 'Work']
    choice = wx.ComboBox(panel, -1, choices=tables)

    sizer.Add(database, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.database_pnl, pos=(0,1), span=(1,2), flag=wx.TOP|wx.EXPAND,
      border=15)
    sizer.Add(choice, pos=(1,0), span=(1,2), flag=wx.TOP|wx.LEFT|wx.EXPAND,
      border=15)

    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
    self.Center()
    self.Show()

  def OnClose(self, event):
    self.Close()

  def OnSelect(self, event):
    item = event.GetSelection()
    if item == 0:
      Food(self, 1, "Food")
    elif item == 1:
      Discretionary(self, 1, "Discretionary")
    elif item == 2:
      Sleep(self, 1, "Sleep")
    elif item == 3:
      Social(self, 1, "Social")
    elif item == 4:
      Work(self, 1, "Work")
    else:
      print "What's all this, then?"

class Food(MainForm, Thread):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(250, 370))
    Thread.__init__(self)
    self.start()

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2, 8)

    commit = wx.Button(panel, 1, 'Commit')
    close = wx.Button(panel, 2, 'Close')

    date = wx.StaticText(panel, label="Date")
    start = wx.StaticText(panel, label="Start Time")
    stop = wx.StaticText(panel, label="Stop Time")
    item1 = wx.StaticText(panel, label="Item 1")
    item2 = wx.StaticText(panel, label="Item 2")
    item3 = wx.StaticText(panel, label="Item 3")
    item4 = wx.StaticText(panel, label="Item 4")

    self.date_pnl = wx.TextCtrl(panel)
    self.start_pnl = wx.TextCtrl(panel)
    self.stop_pnl = wx.TextCtrl(panel)
    self.item1_pnl = wx.TextCtrl(panel)
    self.item2_pnl = wx.TextCtrl(panel)
    self.item3_pnl = wx.TextCtrl(panel)
    self.item4_pnl = wx.TextCtrl(panel)

    sizer.Add(date, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.date_pnl, pos=(0, 1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(start, pos=(1,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_pnl, pos=(1,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop, pos=(2,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_pnl, pos=(2,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(item1, pos=(3,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.item1_pnl, pos=(3,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(item2, pos=(4,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.item2_pnl, pos=(4,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(item3, pos=(5,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.item3_pnl, pos=(5,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(item4, pos=(6,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.item4_pnl, pos=(6,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(commit, pos=(7,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(close, pos=(7,1), flag=wx.TOP|wx.RIGHT, border=19)

    sizer.AddGrowableCol(1)
    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_BUTTON, self.OnCommit, id=1)
    self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)

    db = parent.database_pnl.GetValue()
    cursor.execute("CREATE DATABASE IF NOT EXISTS " + db)
    cursor.execute("USE " + db)
    cursor.execute("CREATE TABLE IF NOT EXISTS Food ( \
      Id INTEGER AUTO_INCREMENT, \
      Date DATE NOT NULL, \
      StartTime TIME NOT NULL, \
      StopTime TIME NOT NULL, \
      Item1 VARCHAR(30) NOT NULL, \
      Item2 VARCHAR(30), \
      Item3 VARCHAR(30), \
      Item4 VARCHAR(30), \
      PRIMARY KEY (Id))")

    self.Show()

  def OnCommit(self, event):
    head = "INSERT INTO Food(Date, StartTime, StopTime, Item1, Item2, \
      Item3, Item4) VALUES (%s, %s, %s, %s, %s, %s, %s)"
    date = self.date_pnl.GetValue()
    start = self.start_pnl.GetValue()
    stop = self.stop_pnl.GetValue()
    item1 = self.item1_pnl.GetValue()
    item2 = self.item2_pnl.GetValue()
    item3 = self.item3_pnl.GetValue()
    item4 = self.item4_pnl.GetValue()

    if valid_date(date) == None:
      wx.MessageBox('A valid date is required', 'Input Error', wx.OK)
      return
    if valid_time(start) == None:
      wx.MessageBox('A start time is required', 'Input Error', wx.OK)
      return
    if valid_time(stop) == None:
      wx.MessageBox('A stop time is required', 'Input Error', wx.OK)
      return
    if item1 == '':
      wx.MessageBox('At least one item is required', 'Input Error', wx.OK)
      return

    if item2 == '':
      cursor.execute(head, (date, start, stop, item1, None, None, None))
    elif item3 == '':
      cursor.execute(head, (date, start, stop, item1, item2, None, None))
    elif item4 == '':
      cursor.execute(head, (date, start, stop, item1, item2, item3, None))
    else:
      cursor.execute(head, (date, start, stop, item1, item2, item3, item4))

    self.date_pnl.SetValue('')
    self.start_pnl.SetValue('')
    self.stop_pnl.SetValue('')
    self.item1_pnl.SetValue('')
    self.item2_pnl.SetValue('')
    self.item3_pnl.SetValue('')
    self.item4_pnl.SetValue('')

  def OnClose(self, event):
    self.Close()

class Discretionary(MainForm, Thread):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(250, 300))
    Thread.__init__(self)
    self.start()

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2, 2)

    commit = wx.Button(panel, 1, 'Commit')
    close = wx.Button(panel, 2, 'Close')

    date = wx.StaticText(panel, label="Date")
    start = wx.StaticText(panel, label="Start Time")
    stop = wx.StaticText(panel, label="Stop Time")
    activity = wx.StaticText(panel, label="Activity")
    intensity = wx.StaticText(panel, label="Intensity")

    self.date_pnl = wx.TextCtrl(panel)
    self.start_pnl = wx.TextCtrl(panel)
    self.stop_pnl = wx.TextCtrl(panel)
    self.activity_pnl = wx.TextCtrl(panel)
    self.intensity_pnl = wx.TextCtrl(panel)

    sizer.Add(date, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.date_pnl, pos=(0,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(start, pos=(1,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_pnl, pos=(1,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop, pos=(2,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_pnl, pos=(2,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(activity, pos=(3,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.activity_pnl, pos=(3,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(intensity, pos=(4,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.intensity_pnl, pos=(4,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(commit, pos=(5,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(close, pos=(5,1), flag=wx.TOP|wx.RIGHT, border=19)

    sizer.AddGrowableCol(1)
    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_BUTTON, self.OnCommit, id=1)
    self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)

    db = parent.database_pnl.GetValue()
    cursor.execute("CREATE DATABASE IF NOT EXISTS " + db)
    cursor.execute("USE " + db)
    cursor.execute("CREATE TABLE IF NOT EXISTS Discretionary ( \
      Id INTEGER AUTO_INCREMENT, \
      Date DATE NOT NULL, \
      StartTime TIME NOT NULL, \
      StopTime TIME NOT NULL, \
      Activity VARCHAR(64) NOT NULL, \
      Intensity INTEGER, \
      PRIMARY KEY (Id))")

    self.Show()

  def OnCommit(self, event):
    head = "INSERT INTO Discretionary(Date, StartTime, StopTime, Activity, \
      Intensity) VALUES (%s, %s, %s, %s, %s)"
    date = self.date_pnl.GetValue()
    start = self.start_pnl.GetValue()
    stop = self.stop_pnl.GetValue()
    activity = self.activity_pnl.GetValue()
    intensity = self.intensity_pnl.GetValue()

    if valid_date(date) == None:
      wx.MessageBox('A valid date is required', 'Input Error', wx.OK)
      return
    if valid_time(start) == None:
      wx.MessageBox('A valid start time is required', 'Input Error', wx.OK)
      return
    if valid_time(stop) == None:
      wx.MessageBox('A valid stop time is required', 'Input Error', wx.OK)
      return
    if activity == '':
      wx.MessageBox('At least one activity is required', 'Input Error', wx.OK)
      return

    if intensity == '':
      cursor.execute(head, (date, start, stop, activity, None))
    else:
      cursor.execute(head, (date, start, stop,activity, intensity))

    self.date_pnl.SetValue('')
    self.start_pnl.SetValue('')
    self.stop_pnl.SetValue('')
    self.activity_pnl.SetValue('')
    self.intensity_pnl.SetValue('')

  def OnClose(self, event):
    self.Close()

class Sleep(MainForm, Thread):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(220, 280))
    Thread.__init__(self)
    self.start()

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2, 5)

    commit = wx.Button(panel, 1, 'Commit')
    close = wx.Button(panel, 2, 'Close')

    start_date = wx.StaticText(panel, label="Start Date")
    start_time = wx.StaticText(panel, label="Start Time")
    stop_date = wx.StaticText(panel, label="Stop Date")
    stop_time = wx.StaticText(panel, label="Stop Time")

    self.start_date_pnl = wx.TextCtrl(panel)
    self.start_time_pnl = wx.TextCtrl(panel)
    self.stop_date_pnl = wx.TextCtrl(panel)
    self.stop_time_pnl = wx.TextCtrl(panel)

    sizer.Add(start_date, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_date_pnl, pos=(0,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(start_time, pos=(1,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_time_pnl, pos=(1,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_date, pos=(2,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_date_pnl, pos=(2,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_time, pos=(3,0), flag=wx.TOP|wx.LEFT|wx.EXPAND, border=19)
    sizer.Add(self.stop_time_pnl, pos=(3,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(commit, pos=(4,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(close, pos=(4,1), flag=wx.TOP|wx.RIGHT, border=19)

    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_BUTTON, self.OnCommit, id=1)
    self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)

    db = parent.database_pnl.GetValue()
    cursor.execute("CREATE DATABASE IF NOT EXISTS " + db)
    cursor.execute("USE " + db)
    cursor.execute("CREATE TABLE IF NOT EXISTS Sleep ( \
      Id INTEGER AUTO_INCREMENT, \
      StartDate DATE NOT NULL, \
      StartTime TIME NOT NULL, \
      StopDate DATE NOT NULL, \
      StopTime TIME NOT NULL, \
      PRIMARY KEY (Id))")

    self.Show()

  def OnCommit(self, event):
    head = "INSERT INTO Sleep(StartDate, StartTime, StopDate, \
      StopTime) VALUES (%s, %s, %s, %s)"
    start_date = self.start_date_pnl.GetValue()
    start_time = self.start_time_pnl.GetValue()
    stop_date = self.stop_date_pnl.GetValue()
    stop_time = self.stop_time_pnl.GetValue()

    if valid_date(start_date) == None:
      wx.MessageBox('A valid start date is required', 'Input Error', wx.OK)
      return
    if valid_time(start_time) == None:
      wx.MessageBox('A valid start time is required', 'Input Error', wx.OK)
      return
    if valid_date(stop_date) == None:
      wx.MessageBox('A valid stop date is required', 'Input Error', wx.OK)
      return
    if valid_time(stop_time) == None:
      wx.MessageBox('A valid stop time is required', 'Input Error', wx.OK)
      return

    cursor.execute(head, (start_date, start_time, stop_date, stop_time))

    self.start_date_pnl.SetValue('')
    self.start_time_pnl.SetValue('')
    self.stop_date_pnl.SetValue('')
    self.stop_time_pnl.SetValue('')

  def OnClose(self, event):
    self.Close()

class Social(MainForm, Thread):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(220, 460))
    Thread.__init__(self)
    self.start()

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2, 10)

    commit = wx.Button(panel, 1, 'Commit')
    close = wx.Button(panel, 2, 'Close')

    start_date = wx.StaticText(panel, label="Start Date")
    start_time = wx.StaticText(panel, label="Start Time")
    stop_date = wx.StaticText(panel, label="Stop Date")
    stop_time = wx.StaticText(panel, label="Stop Time")
    medium = wx.StaticText(panel, label="Medium")
    other1 = wx.StaticText(panel, label="Other 1")
    other2 = wx.StaticText(panel, label="Other 2")
    other3 = wx.StaticText(panel, label="Other 3")
    other4 = wx.StaticText(panel, label="Other 4")

    self.start_date_pnl = wx.TextCtrl(panel)
    self.start_time_pnl = wx.TextCtrl(panel)
    self.stop_date_pnl = wx.TextCtrl(panel)
    self.stop_time_pnl = wx.TextCtrl(panel)
    self.medium_pnl = wx.TextCtrl(panel)
    self.other1_pnl = wx.TextCtrl(panel)
    self.other2_pnl = wx.TextCtrl(panel)
    self.other3_pnl = wx.TextCtrl(panel)
    self.other4_pnl = wx.TextCtrl(panel)

    sizer.Add(start_date, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_date_pnl, pos=(0,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(start_time, pos=(1,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_time_pnl, pos=(1,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_date, pos=(2,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_date_pnl, pos=(2,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_time, pos=(3,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_time_pnl, pos=(3,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(medium, pos=(4,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.medium_pnl, pos=(4,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(other1, pos=(5,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.other1_pnl, pos=(5,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(other2, pos=(6,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.other2_pnl, pos=(6,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(other3, pos=(7,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.other3_pnl, pos=(7,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(other4, pos=(8,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.other4_pnl, pos=(8,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(commit, pos=(9,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(close, pos=(9,1), flag=wx.TOP|wx.RIGHT, border=19)

    sizer.AddGrowableCol(1)
    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_BUTTON, self.OnCommit, id=1)
    self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)

    db = parent.database_pnl.GetValue()
    cursor.execute("CREATE DATABASE IF NOT EXISTS " + db)
    cursor.execute("USE " + db)
    cursor.execute("CREATE TABLE IF NOT EXISTS Social ( \
      Id INTEGER AUTO_INCREMENT, \
      StartDate DATE NOT NULL, \
      StartTime TIME NOT NULL, \
      StopDate DATE NOT NULL, \
      StopTime TIME NOT NULL, \
      Medium VARCHAR(32) NOT NULL, \
      Other1 VARCHAR(32) NOT NULL, \
      Other2 VARCHAR(32), \
      Other3 VARCHAR(32), \
      Other4 VARCHAR(32), \
      PRIMARY KEY (Id))")

    self.Show()

  def OnCommit(self, event):
    head = "INSERT INTO Social(StartDate, StartTime, StopDate, StopTime, \
      Medium, Other1, Other2, Other3, Other4) VALUES (%s, %s, %s, %s, %s, %s, \
      %s, %s, %s)"
    required_head = "INSERT INTO Social(StartDate, StartTime, StopDate, \
      StopTime, Medium, Other1"
    start_date = self.start_date_pnl.GetValue()
    start_time = self.start_time_pnl.GetValue()
    stop_date = self.stop_date_pnl.GetValue()
    stop_time = self.stop_time_pnl.GetValue()
    medium = self.medium_pnl.GetValue()
    other1 = self.other1_pnl.GetValue()
    other2 = self.other2_pnl.GetValue()
    other3 = self.other3_pnl.GetValue()
    other4 = self.other4_pnl.GetValue()

    if valid_date(start_date) == None:
      wx.MessageBox('A valid start date is required', 'Input Error', wx.OK)
      return
    if valid_time(start_time) == None:
      wx.MessageBox('A valid start time is required', 'Input Error', wx.OK)
      return
    if valid_date(stop_date) == None:
      wx.MessageBox('A valid stop date is required', 'Input Error', wx.OK)
      return
    if valid_time(stop_time) == None:
      wx.MessageBox('A valid stop time is required', 'Input Error', wx.OK)
      return
    if medium == '':
      wx.MessageBox('A medium of communication is required', 'Input Error',
        wx.OK)
      return
    if other1 == '':
      wx.MessageBox('At least one other person is required', 'Input Error',
        wx.OK)
      return

    if other2 == '':
      cursor.execute(head, (start_date, start_time, stop_date, stop_time,
        medium, other1, None, None, None))
    elif other3 == '':
      cursor.execute(head, (start_date, start_time, stop_date, stop_time,
        medium, other1, other2, None, None))
    elif other4 == '':
      cursor.execute(head, (start_date, start_time, stop_date, stop_time,
        medium, other1, other2, other3, None))
    else:
      cursor.execute(head, (start_date, start_time, stop_date, stop_time,
        medium, other1, other2, other3, other4))

    self.start_date_pnl.SetValue('')
    self.start_time_pnl.SetValue('')
    self.stop_date_pnl.SetValue('')
    self.stop_time_pnl.SetValue('')
    self.medium_pnl.SetValue('')
    self.other1_pnl.SetValue('')
    self.other2_pnl.SetValue('')
    self.other3_pnl.SetValue('')
    self.other4_pnl.SetValue('')

  def OnClose(self, event):
    self.Close()

class Work(MainForm, Thread):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(220, 320))
    Thread.__init__(self)
    self.start()

    panel = wx.Panel(self)
    sizer = wx.GridBagSizer(2, 6)

    commit = wx.Button(panel, 1, 'Commit')
    close = wx.Button(panel, 2, 'Close')

    start_date = wx.StaticText(panel, label="Start Date")
    start_time = wx.StaticText(panel, label="Start Time")
    stop_date = wx.StaticText(panel, label="Stop Date")
    stop_time = wx.StaticText(panel, label="Stop Time")
    motivation = wx.StaticText(panel, label="Motivation")

    self.start_date_pnl = wx.TextCtrl(panel)
    self.start_time_pnl = wx.TextCtrl(panel)
    self.stop_date_pnl = wx.TextCtrl(panel)
    self.stop_time_pnl = wx.TextCtrl(panel)
    self.motivation_pnl = wx.TextCtrl(panel)

    sizer.Add(start_date, pos=(0,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_date_pnl, pos=(0,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(start_time, pos=(1,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.start_time_pnl, pos=(1,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_date, pos=(2,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(self.stop_date_pnl, pos=(2,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(stop_time, pos=(3,0), flag=wx.TOP|wx.LEFT|wx.EXPAND, border=19)
    sizer.Add(self.stop_time_pnl, pos=(3,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(motivation, pos=(4,0), flag=wx.TOP|wx.LEFT|wx.EXPAND, border=19)
    sizer.Add(self.motivation_pnl, pos=(4,1), flag=wx.TOP|wx.RIGHT|wx.EXPAND,
      border=15)

    sizer.Add(commit, pos=(5,0), flag=wx.TOP|wx.LEFT, border=19)
    sizer.Add(close, pos=(5,1), flag=wx.TOP|wx.RIGHT, border=19)

    panel.SetSizerAndFit(sizer)

    self.Bind(wx.EVT_BUTTON, self.OnCommit, id=1)
    self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)

    db = parent.database_pnl.GetValue()
    cursor.execute("CREATE DATABASE IF NOT EXISTS " + db)
    cursor.execute("USE " + db)
    cursor.execute("CREATE TABLE IF NOT EXISTS Work ( \
      Id INTEGER AUTO_INCREMENT, \
      StartDate DATE NOT NULL, \
      StartTime TIME NOT NULL, \
      StopDate DATE NOT NULL, \
      StopTime TIME NOT NULL, \
      Motivation INTEGER, \
      PRIMARY KEY (Id))")

    self.Show()

  def OnCommit(self, event):
    head = "INSERT INTO Work(StartDate, StartTime, StopDate, StopTime, \
      Motivation) VALUES (%s, %s, %s, %s, %s)"
    start_date = self.start_date_pnl.GetValue()
    start_time = self.start_time_pnl.GetValue()
    stop_date = self.stop_date_pnl.GetValue()
    stop_time = self.stop_time_pnl.GetValue()
    motivation = self.motivation_pnl.GetValue()

    if valid_date(start_date) == None:
      wx.MessageBox('A valid start date is required', 'Input Error', wx.OK)
      return
    if valid_time(start_time) == None:
      wx.MessageBox('A valid start time is required', 'Input Error', wx.OK)
      return
    if valid_date(stop_date) == None:
      wx.MessageBox('A valid stop date is required', 'Input Error', wx.OK)
      return
    if valid_time(stop_time) == None:
      wx.MessageBox('A valid stop time is required', 'Input Error', wx.OK)
      return

    if motivation == '':
      cursor.execute(head, (start_date, start_time, stop_date, stop_time, None))
    else:
      cursor.execute(head, (start_date, start_time, stop_date, stop_time,
        motivation))

    self.start_date_pnl.SetValue('')
    self.start_time_pnl.SetValue('')
    self.stop_date_pnl.SetValue('')
    self.stop_time_pnl.SetValue('')
    self.motivation_pnl.SetValue('')

  def OnClose(self, event):
    self.Close()

app = wx.App()
MainForm(None, -1, 'Quantified Self')
app.MainLoop()
